package com.globallogic.oopbasics.competence.entity;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.globallogic.oopbasics.competence.entity.creator.CompetencyValuesParser;

/**
 * @author olena.strelchuk
 */

/**
 * Competency is an adequate characteristic of personality's ability to perform
 * some actions in working activities
 */
public class CompetencyValue {

	private AbstractCompetency competency;

	private List<Double> paramValues = new ArrayList<Double>();

	private Map<String, Double> parseParams() {
		CompetencyValuesParser parser = new CompetencyValuesParser(
				competency.getParamList(), paramValues);
		return parser.getParamMap();
	}

	/**
	 * Constructor for CompetencyValue
	 */
	public CompetencyValue(AbstractCompetency competency, Double... values) {
		this.setCompetency(competency);
		for (int i = 0; i < values.length; i++) {
			paramValues.add(values[i]);
		}
	}

	/**
	 * toString Method for Competency
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return competency.getName() + " -- " + competency.getGlobalValue()
				+ " * " + parseParams().toString() + " = " + getTotalValue();
	}

	double roundFourDecimals(double d) {
		DecimalFormat fourDForm = new DecimalFormat("#.####");
		return Double.valueOf(fourDForm.format(d));
	}

	public double getTotalValue() {
		double value = 0;
		Iterator<Double> iterator = paramValues.iterator();
		while (iterator.hasNext()) {
			value += iterator.next();
		}
		return roundFourDecimals(value * competency.getGlobalValue());
	}

	public void setCompetency(AbstractCompetency competency) {
		this.competency = competency;
	}

}
